home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML Instance.sea / XML Instance / Required / plugins / HTMLWindow.jar / horst / FrameSetLayout.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-03-18  |  7.4 KB  |  301 lines

  1. package horst;
  2.  
  3. import java.awt.Component;
  4. import java.awt.Container;
  5. import java.awt.Dimension;
  6. import java.awt.Insets;
  7. import java.awt.LayoutManager;
  8. import java.awt.Point;
  9. import java.awt.Rectangle;
  10. import java.util.Hashtable;
  11. import java.util.Vector;
  12.  
  13. public class FrameSetLayout implements LayoutManager {
  14.    static final int HORIZONTAL = 0;
  15.    static final int VERTICAL = 1;
  16.    private int _orientation = 0;
  17.    private Hashtable _hash = new Hashtable();
  18.    private int _lastWidth = -1;
  19.    private int _lastHeight = -1;
  20.    private int[] pctAtts;
  21.    private int[] wildAtts;
  22.    private int[] pixAtts;
  23.    private boolean _bAlwaysConstraintLayout = false;
  24.  
  25.    public void addLayoutComponent(Component comp, Object constraints) {
  26.       this._hash.put(comp, constraints);
  27.    }
  28.  
  29.    public void addLayoutComponent(String name, Component comp) {
  30.    }
  31.  
  32.    private void constraintLayout(Container parent) {
  33.       Insets insets = parent.getInsets();
  34.       Dimension dim = ((Component)parent).getSize();
  35.       int top = insets.top;
  36.       int bottom = dim.height - insets.bottom;
  37.       int left = insets.left;
  38.       int right = dim.width - insets.right;
  39.       int space = this._orientation == 0 ? right - left : bottom - top;
  40.       Component[] comps = parent.getComponents();
  41.       Vector temp = new Vector();
  42.       int nSplitBars = 0;
  43.       int splitBarSpace = 0;
  44.  
  45.       for(int i = 0; i < comps.length; ++i) {
  46.          if (!(comps[i] instanceof FrameSplitterBar)) {
  47.             temp.addElement(comps[i]);
  48.          } else {
  49.             int splitBarWidth = ((FrameSplitterBar)comps[i]).getBarSize();
  50.             splitBarSpace += splitBarWidth;
  51.             ((FrameSplitterBar)comps[i]).setOrientation(this._orientation);
  52.             if (this._orientation == 0) {
  53.                comps[i].setBounds(left, top, splitBarWidth, dim.height);
  54.             } else {
  55.                comps[i].setBounds(left, top, dim.width, splitBarWidth);
  56.             }
  57.  
  58.             ++nSplitBars;
  59.          }
  60.       }
  61.  
  62.       space = Math.max(0, space - splitBarSpace);
  63.       int nFrames = temp.size();
  64.       Component[] frames = new Component[nFrames];
  65.       temp.copyInto(frames);
  66.       int pixSpace = this.getPixelSpace(frames);
  67.       int pctSpace = this.getPercentSpace(frames, space);
  68.       this.getWildcardDivisions(frames);
  69.       int wildcardSpace = Math.max(0, space - pctSpace - pixSpace);
  70.  
  71.       for(int i = 0; i < frames.length; ++i) {
  72.          FrameConstraint c = (FrameConstraint)this._hash.get(frames[i]);
  73.          if (c != null && c.type == 1) {
  74.             int sz = (int)(c.percent * (float)space);
  75.             if (this._orientation == 0) {
  76.                frames[i].setBounds(0, 0, sz, dim.height);
  77.             } else {
  78.                frames[i].setBounds(0, 0, dim.width, sz);
  79.             }
  80.          }
  81.       }
  82.  
  83.       for(int i = 0; i < frames.length; ++i) {
  84.          FrameConstraint c = (FrameConstraint)this._hash.get(frames[i]);
  85.          if (c != null && c.type == 0) {
  86.             if (this._orientation == 0) {
  87.                frames[i].setBounds(0, 0, c.pixels, dim.height);
  88.             } else {
  89.                frames[i].setBounds(0, 0, dim.width, c.pixels);
  90.             }
  91.          }
  92.       }
  93.  
  94.       int leftOverSpace = wildcardSpace;
  95.       int nDivisions = this.getWildcardDivisions(frames);
  96.       int divisionWidth = 0;
  97.       if (nDivisions > 0) {
  98.          divisionWidth = wildcardSpace / nDivisions;
  99.  
  100.          for(int i = 0; i < frames.length; ++i) {
  101.             FrameConstraint c = (FrameConstraint)this._hash.get(frames[i]);
  102.             if (c != null && c.type == 2) {
  103.                int sz = c.wildcard * divisionWidth;
  104.                if (this._orientation == 0) {
  105.                   frames[i].setBounds(0, 0, sz, dim.height);
  106.                } else {
  107.                   frames[i].setBounds(0, 0, dim.width, sz);
  108.                }
  109.             }
  110.          }
  111.       } else if (wildcardSpace > 0) {
  112.          if (wildcardSpace < nFrames) {
  113.             for(int i = 0; i < leftOverSpace; ++i) {
  114.                Rectangle b = frames[i].getBounds();
  115.                if (this._orientation == 0) {
  116.                   frames[i].setBounds(0, 0, b.width + 1, dim.height);
  117.                } else {
  118.                   frames[i].setBounds(0, 0, dim.width, b.height + 1);
  119.                }
  120.             }
  121.          } else {
  122.             double pct = (double)1.0F / (double)nFrames;
  123.             int runningTotal = 0;
  124.             int addAmount = 0;
  125.  
  126.             for(int i = 0; i < nFrames; ++i) {
  127.                if (i == nFrames - 1) {
  128.                   addAmount = leftOverSpace - runningTotal;
  129.                } else {
  130.                   addAmount = (int)(pct * (double)((float)leftOverSpace));
  131.                }
  132.  
  133.                runningTotal += addAmount;
  134.                Rectangle b = frames[i].getBounds();
  135.                if (this._orientation == 0) {
  136.                   frames[i].setBounds(0, 0, b.width + addAmount, dim.height);
  137.                } else {
  138.                   frames[i].setBounds(0, 0, dim.width, b.height + addAmount);
  139.                }
  140.             }
  141.          }
  142.       }
  143.  
  144.       int x = left;
  145.       int y = top;
  146.  
  147.       for(int i = 0; i < comps.length; ++i) {
  148.          comps[i].setLocation(x, y);
  149.          if (this._orientation == 0) {
  150.             x += comps[i].getBounds().width;
  151.          } else {
  152.             y += comps[i].getBounds().height;
  153.          }
  154.       }
  155.  
  156.       this._lastWidth = dim.width;
  157.       this._lastHeight = dim.height;
  158.    }
  159.  
  160.    void doConstraintLayout() {
  161.       this._lastWidth = -1;
  162.       this._lastHeight = -1;
  163.    }
  164.  
  165.    public float getLayoutAlignmentX(Container target) {
  166.       return 0.5F;
  167.    }
  168.  
  169.    public float getLayoutAlignmentY(Container target) {
  170.       return 0.5F;
  171.    }
  172.  
  173.    private int getPercentSpace(Component[] comps, int space) {
  174.       int pctSpace = 0;
  175.  
  176.       for(int i = 0; i < comps.length; ++i) {
  177.          FrameConstraint c = (FrameConstraint)this._hash.get(comps[i]);
  178.          if (c != null && c.type == 1) {
  179.             pctSpace = (int)((float)pctSpace + (float)space * c.percent);
  180.          }
  181.       }
  182.  
  183.       return pctSpace;
  184.    }
  185.  
  186.    private int getPixelSpace(Component[] comps) {
  187.       int total = 0;
  188.  
  189.       for(int i = 0; i < comps.length; ++i) {
  190.          FrameConstraint c = (FrameConstraint)this._hash.get(comps[i]);
  191.          if (c != null && c.type == 0) {
  192.             total += c.pixels;
  193.          }
  194.       }
  195.  
  196.       return total;
  197.    }
  198.  
  199.    private int getWildcardDivisions(Component[] comps) {
  200.       int nDivisions = 0;
  201.  
  202.       for(int i = 0; i < comps.length; ++i) {
  203.          FrameConstraint c = (FrameConstraint)this._hash.get(comps[i]);
  204.          if (c != null && c.type == 2) {
  205.             nDivisions += c.wildcard;
  206.          }
  207.       }
  208.  
  209.       return nDivisions;
  210.    }
  211.  
  212.    public void invalidateLayout(Container target) {
  213.    }
  214.  
  215.    public void layoutContainer(Container parent) {
  216.       if (!this._bAlwaysConstraintLayout && (this._orientation != 0 || this._lastWidth != -1) && (this._orientation != 1 || this._lastHeight != -1)) {
  217.          this.splitBarLayout(parent);
  218.       } else {
  219.          this.constraintLayout(parent);
  220.       }
  221.  
  222.    }
  223.  
  224.    public Dimension maximumLayoutSize(Container target) {
  225.       return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
  226.    }
  227.  
  228.    public Dimension minimumLayoutSize(Container parent) {
  229.       return ((Component)parent).getSize();
  230.    }
  231.  
  232.    public Dimension preferredLayoutSize(Container parent) {
  233.       return ((Component)parent).getSize();
  234.    }
  235.  
  236.    public void removeLayoutComponent(Component comp) {
  237.    }
  238.  
  239.    void setAlwaysConstraintLayout(boolean bAlways) {
  240.       this._bAlwaysConstraintLayout = bAlways;
  241.    }
  242.  
  243.    void setConstraints(Component c, FrameConstraint constraints) {
  244.       this._hash.put(c, constraints);
  245.    }
  246.  
  247.    void setOrientation(int orientation) {
  248.       this._orientation = orientation;
  249.    }
  250.  
  251.    private void splitBarLayout(Container parent) {
  252.       Insets insets = parent.getInsets();
  253.       Dimension dim = ((Component)parent).getSize();
  254.       int top = insets.top;
  255.       int bottom = dim.height - insets.bottom;
  256.       int left = insets.left;
  257.       int right = dim.width - insets.right;
  258.       int var10000 = right - left;
  259.       Component[] comps = parent.getComponents();
  260.       int nFrames = 0;
  261.  
  262.       for(int i = 0; i < comps.length; ++i) {
  263.          if (!(comps[i] instanceof FrameSplitterBar)) {
  264.             ++nFrames;
  265.          }
  266.       }
  267.  
  268.       for(int i = 0; i < comps.length; ++i) {
  269.          Component c = comps[i];
  270.          if (c instanceof FrameSplitterBar) {
  271.             Rectangle r = c.getBounds();
  272.             int sz = ((FrameSplitterBar)c).getBarSize();
  273.             if (this._orientation == 0) {
  274.                c.setBounds(r.x, top, sz, dim.height);
  275.             } else {
  276.                c.setBounds(left, r.y, dim.width, sz);
  277.             }
  278.          } else if (i + 1 < comps.length && comps[i + 1] instanceof FrameSplitterBar) {
  279.             int barWidth = ((FrameSplitterBar)comps[i + 1]).getBarSize();
  280.             Point splitterPos = comps[i + 1].getLocation();
  281.             if (this._orientation == 0) {
  282.                int frameWidth = splitterPos.x - left;
  283.                c.setBounds(left, top, frameWidth, dim.height);
  284.                left += frameWidth + barWidth;
  285.             } else {
  286.                int frameHeight = splitterPos.y - top;
  287.                c.setBounds(left, top, dim.width, frameHeight);
  288.                top += frameHeight + barWidth;
  289.             }
  290.          } else if (i == comps.length - 1) {
  291.             if (this._orientation == 0) {
  292.                c.setBounds(left, top, right - left, dim.height);
  293.             } else {
  294.                c.setBounds(left, top, dim.width, bottom - top);
  295.             }
  296.          }
  297.       }
  298.  
  299.    }
  300. }
  301.